package com.vincent.kdp.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.vincent.kdp.PostUrl;
import com.vincent.kdp.R;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.js.CostOrderDetailOperator;
import com.vincent.kdp.js.CostOrderOperator;
import com.vincent.kdp.ui.bean.response.BaseOutput;
import com.vincent.kdp.ui.bean.response.GetSalesRecordOutput;
import com.vincent.kdp.ui.commodity.repository.CommodityDetailRepository;
import com.vincent.kdp.util.DialogUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 消费订单详情
 * Created by pg on 2017/1/23.
 */

public class CostDetailActivity extends AppCompatActivity {

    @Bind(R.id.back_img)
    ImageView mBackImg;
    @Bind(R.id.toolbar_center_tv)
    TextView mToolbarCenterTv;
    @Bind(R.id.webView)
    WebView webView;

    public CommodityDetailRepository repository = new CommodityDetailRepository();

    private Activity thisActivity;

    private String consumeNumber = "";

    public static Activity currentActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_costorderdetail);
        ButterKnife.bind(this);
        mToolbarCenterTv.setText("消费详情");

        if (currentActivity != null) {
            currentActivity.finish();
        }
        currentActivity = this;

        consumeNumber = getIntent().getStringExtra("consumeNumber");

        if (consumeNumber == null || consumeNumber.isEmpty()) {

            String barCode = getIntent().getStringExtra("barCode");
            getConsumeNumber(barCode);
        } else {
            this.initializeWebView();
        }
    }


    private void getConsumeNumber(String barCode) {
        repository.getSalesRecordByBarCode(barCode, new RequestCallBack<GetSalesRecordOutput>() {
            @Override
            public void onSuccess(GetSalesRecordOutput object) {
                if (object.isSuccess()) {
                    consumeNumber = object.getData();
                    initializeWebView();
                }
            }

            @Override
            public void onFalse(String msg) {
                DialogUtils.getIsInstance().createDialog(msg, thisActivity);

                thisActivity.finish();
            }
        });
    }


    @SuppressLint({"NewApi", "SetJavaScriptEnabled"})
    private void initializeWebView() {

        webView.addJavascriptInterface(new CostOrderDetailOperator(CostDetailActivity.this, consumeNumber),
                "CostDetail");
        try {
            //String url = "file:///android_asset/LoginJs/login.html";
            String url = PostUrl.SERVICE_URL + "/H5/costDetail/costDetail.html";
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setAllowFileAccess(true);
            webSettings.setAllowFileAccessFromFileURLs(true);
            setClient();
            this.webView.loadUrl(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.back_img)
    public void onClick() {
        this.finish();
        overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
    }

    private void setClient() {
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                AlertDialog.Builder b2 = new AlertDialog.Builder(CostDetailActivity.this).setTitle("通知")
                        .setMessage(message).setPositiveButton("ok", new AlertDialog.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result.confirm();
                            }
                        });
                b2.setCancelable(false);
                b2.create();
                b2.show();
                return true;
            }
        });
    }
}
